package org.springframework.ApplicationContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author jiangjianmin
 * @version 1.0
 * @description: 自定义容器
 * @date 2024/7/1 20:17
 */
public class MyselfClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {
	/**
	 * Create a new ClassPathXmlApplicationContext, loading the definitions
	 * from the given XML file and automatically refreshing the context.
	 * @param configLocations resource location
	 * @throws BeansException if context creation failed
	 */
	public MyselfClassPathXmlApplicationContext(String... configLocations) throws BeansException {
		super(configLocations);
	}

	/**
	 *
	 * Spring refresh 方法第扩展点加一：refresh方法中的prepareRefresh();
	 * 重写initPropertySources 设置环境变量和设置requiredProperties
	 *
	 */
	@Override
	protected void initPropertySources() {
		System.out.println("自定义 initPropertySources");
		getEnvironment().getSystemProperties().put("systemOS", "mac");
		getEnvironment().setRequiredProperties("systemOS");
	}
	/**
	 *
	 * Spring refresh 方法第扩展点加一：refresh方法中的postProcessBeanFactory();
	 * 重写该方法，修改基础容器BeanFactory内容
	 * @param beanFactory
	 *
	 */
	@Override
	protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		System.out.println("自定义 postProcessBeanFactory");
		// 获取容器中的对象
//		BeanDefinition beanDefinition = beanFactory.getBeanDefinition("jmUser");
//		beanDefinition.getScope();
//		// 修改属性
//		beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);

	}
}
